<template>
  <el-card class="login-container">
    <el-form :model="form" label-width="120px">
      <el-form-item label="用户名">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { reactive } from 'vue'
import { loginAPI } from '@/api'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import msgUtis from '@/utils/message.js'
const userStore = useUserStore()
const router = useRouter()
const form = reactive({
  name: '',
  password: ''
})

const onSubmit = async () => {
  const res = await loginAPI(form)
  if (res.code == 200) {
    userStore.setUser(res.data)
    router.push("/")
    msgUtis('登录成功')
  } else {
    msgUtis('登录失败', 'error')
  }
}
</script>

<style scoped lang="scss">
.login-container {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
